package com.dejiao.controller;


import com.dejiao.entity.ResultEntity;
import com.dejiao.entity.po.PasswordEntity;
import com.dejiao.entity.vo.PasswordEntityVo;
import com.dejiao.entity.vo.WebSiteVo;
import com.dejiao.myenum.ResponseEnum;
import com.dejiao.service.PeService;
import com.dejiao.utils.JWT;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.util.UriComponentsBuilder;

import java.util.List;

/**
 * Created by Germmy on 2018/3/31.
 */
@RestController
@RequestMapping("/pmc")
public class PasswordMgrController {


    //添加一个日志器
    private static final Logger logger = LoggerFactory.getLogger(PasswordMgrController.class);

    @Autowired
    private PeService peService;

    /**
     * @api {get} http://xxx/xiaomifeng/pmc/pwdquery/{webSiteName}  根据webSiteName查询密码实体
     * @apiName GetPasswordEntity
     * @apiGroup PasswordEntity
     * @apiParam {String} webSiteName 密码实体的唯一编号
     * @apiSuccess {String} webSiteName 密码实体的唯一编号
     * @apiSuccess {String} webSiteName  网站名称
     * @apiSuccess {String} userName  用户名
     * @apiSuccess {String} password  密码
     * @apiSuccessExample Success-Response:
     * 0000000 OKxxxx
     * {
     * "errCode": "0000000",
     * "errorMessage": "success",
     * "data": {
     * "webSiteName": "jd",
     * "userName": "wandejun1012",
     * "password": "123456",
     * "peId": "abcdefg"
     * }
     * }
     * @apiError 1000001 该密码实体找不到
     * @apiError 1000002 xxxx
     * @apiError 9999999 服务器开小差了
     * @apiErrorExample Error-Response:
     * {
     * "errCode": "1000001",
     * "errorMessage": "该密码实体找不到",
     * "data": null
     * }
     */
    @RequestMapping(value = "/pwdquery", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
//    public ResponseEntity<ResultEntity<PasswordEntity>> getPasswordEntity(@PathVariable("webSiteName") String webSiteName) {
    public ResponseEntity<ResultEntity<PasswordEntity>> getPasswordEntity(@RequestBody WebSiteVo webSiteVo) {
        //输出日志文件
        logger.info("password query start"+webSiteVo);
        ResultEntity<PasswordEntity> resultEntity = new ResultEntity<>();
        //添加校验jwt是否有效的逻辑
        String jwt=webSiteVo.getJwt();
        //判断此jwt是否有效
       if(JWT.checkJwtIsValid(jwt)){
        PasswordEntity pe = peService.selectByWebSiteName(webSiteVo.getWebSiteName().toLowerCase());//一律转为小写
        //设置re
        resultEntity.setErrCode(ResponseEnum.SUCCESS.getErrorCode());
        resultEntity.setErrorMessage(ResponseEnum.SUCCESS.getErrorMessage());
        resultEntity.setData(pe);
       }else{
           //提示请登录
           //设置re
           resultEntity.setErrCode(ResponseEnum.USER_NOT_LOGIN.getErrorCode());
           resultEntity.setErrorMessage(ResponseEnum.USER_NOT_LOGIN.getErrorMessage());
           resultEntity.setData(null);
       }
        logger.info("password query end");
        return new ResponseEntity<>(resultEntity, HttpStatus.OK);
    }



    /**
     * @api {get} http://xxx/pmc/pwdquerylist/{webSiteName}  根据webSiteName模糊查询密码实体
     * @apiName pwdquerylist
     * @apiGroup PasswordEntity
     * @apiParam {String} webSiteName 密码实体的唯一编号
     * @apiSuccess {String} webSiteName  网站名称
     * @apiSuccessExample Success-Response:
     * 0000000 OKxxxx
     * {
     * "errCode": "0000000",
     * "errorMessage": "success",
     * "data": {
     * "webSiteName": "jd",
     * "userName": "wandejun1012",
     * "password": "123456",
     * "peId": "abcdefg"
     * }
     * }
     * @apiError 1000001 该密码实体找不到
     * @apiError 1000002 xxxx
     * @apiError 9999999 服务器开小差了
     * @apiErrorExample Error-Response:
     * {
     * "errCode": "1000001",
     * "errorMessage": "该密码实体找不到",
     * "data": null
     * }
     */
    @RequestMapping(value = "/pwdquerylist", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<ResultEntity<List<String>>> getListPasswordEntity(@RequestBody WebSiteVo webSiteVo) {
        //输出日志文件
        logger.info("password ambigious  query start"+webSiteVo);
        ResultEntity<List<String>> resultEntity = new ResultEntity<>();
        //添加校验jwt是否有效的逻辑
        String jwt=webSiteVo.getJwt();
        //判断此jwt是否有效
        if(JWT.checkJwtIsValid(jwt)){
            List<String> peList = peService.selectListByWebSiteName(webSiteVo.getWebSiteName().toLowerCase());//一律转为小写
            //设置re
            resultEntity.setErrCode(ResponseEnum.SUCCESS.getErrorCode());
            resultEntity.setErrorMessage(ResponseEnum.SUCCESS.getErrorMessage());
            resultEntity.setData(peList);
        }else{
            //提示请登录
            //设置re
            resultEntity.setErrCode(ResponseEnum.USER_NOT_LOGIN.getErrorCode());
            resultEntity.setErrorMessage(ResponseEnum.USER_NOT_LOGIN.getErrorMessage());
            resultEntity.setData(null);
        }
        logger.info("password ambigious end");
        return new ResponseEntity<>(resultEntity, HttpStatus.OK);
    }




    //注意：在调用时必须加上后面的/,否则映射不到
    /**
     * @api {post} http://xxx/xiaomifeng/pmc/pwdCreate  创建密码实体
     * @apiName pwdCreate
     * @apiGroup pwdCreate
     * @apiParam {String} webSiteName  网站名称
     * @apiParam {String} userName  用户名
     * @apiParam {String} password  密码
     * @apiSuccess {Long} id 密码实体的唯一编号
     * @apiSuccess {String} webSiteName  网站名称
     * @apiSuccess {String} userName  用户名
     * @apiSuccess {String} password  密码
     * @apiSuccessExample Success-Response:
     * 0000000 OK
     * {
     * "errCode": "0000000",
     * "errorMessage": "success",
     * "data": {
     * "webSiteName": "jd",
     * "userName": "wandejun1012",
     * "password": "123456",
     * "peId": "abcdefg"
     * }
     * }
     * @apiError 1000011 重复创建
     * @apiError 1000002 xxxx
     * @apiError 9999999 服务器开小差了
     * @apiErrorExample Error-Response:
     * {
     * "errCode": "1000011",
     * "errorMessage": "重复创建",
     * "data": null
     * }
     */
    @RequestMapping(value = "/pwdCreate", method = RequestMethod.POST)
    public ResponseEntity<ResultEntity> createPwd(@RequestBody PasswordEntityVo passwordEntityVo, UriComponentsBuilder ucBuilder) {
        logger.info("Creating PasswordEntity start" + passwordEntityVo);
        ResultEntity resultEntity = new ResultEntity<>();
        //添加一个逻辑判断jwt
        //添加校验jwt是否有效的逻辑
        String jwt=passwordEntityVo.getJwt();
        //判断此jwt是否有效
        if(JWT.checkJwtIsValid(jwt)){
            //构造一个po
            PasswordEntity passwordEntity=new PasswordEntity();
            BeanUtils.copyProperties(passwordEntityVo,passwordEntity);
            passwordEntity.setWebSiteName(passwordEntity.getWebSiteName().toLowerCase());//一律转为小写存进去
            try {
                peService.insert(passwordEntity);
            } catch (DuplicateKeyException e) {
                resultEntity.setErrCode(ResponseEnum.DUPLICATE.getErrorCode());
                resultEntity.setErrorMessage(ResponseEnum.DUPLICATE.getErrorMessage());
                return  new ResponseEntity<>(resultEntity, HttpStatus.OK);
            }
            resultEntity.setErrCode(ResponseEnum.SUCCESS.getErrorCode());
            resultEntity.setErrorMessage(ResponseEnum.SUCCESS.getErrorMessage());
        }else{
            //未登录
            //提示请登录
            //设置re
            resultEntity.setErrCode(ResponseEnum.USER_NOT_LOGIN.getErrorCode());
            resultEntity.setErrorMessage(ResponseEnum.USER_NOT_LOGIN.getErrorMessage());
            resultEntity.setData(null);
        }
        logger.info("Creating PasswordEntity end");
        return new ResponseEntity<>(resultEntity, HttpStatus.OK);
    }



    /**
     * @api {post} http://xxx/xiaomifeng/pmc/pwdUpdate  更新密码实体
     * @apiName pwdCreate
     * @apiGroup pwdCreate
     * @apiParam {String} webSiteName  网站名称
     * @apiParam {String} userName  用户名
     * @apiParam {String} password  密码
     * @apiSuccess {Long} id 密码实体的唯一编号
     * @apiSuccess {String} webSiteName  网站名称
     * @apiSuccess {String} userName  用户名
     * @apiSuccess {String} password  密码
     * @apiSuccessExample Success-Response:
     * 0000000 OK
     * {
     * "errCode": "0000000",
     * "errorMessage": "success",
     * "data": {
     * "webSiteName": "jd",
     * "userName": "wandejun1012",
     * "password": "123456",
     * "peId": "abcdefg"
     * }
     * }
     * @apiError 1000011 重复创建
     * @apiError 1000002 xxxx
     * @apiError 9999999 服务器开小差了
     * @apiErrorExample Error-Response:
     * {
     * "errCode": "1000011",
     * "errorMessage": "重复创建",
     * "data": null
     * }
     */
    @RequestMapping(value = "/pwdUpdate", method = RequestMethod.POST)
    public ResponseEntity<ResultEntity> updatePwd(@RequestBody PasswordEntityVo passwordEntityVo) {
        logger.info("Updateting PasswordEntity start" + passwordEntityVo);
        ResultEntity<PasswordEntity> resultEntity = new ResultEntity<>();

        //1.从vo中拿出webSiteName、新的用户名、密码
        //2.直接调用update命令

        //添加校验jwt是否有效的逻辑
        String jwt=passwordEntityVo.getJwt();
        //判断此jwt是否有效
        if(JWT.checkJwtIsValid(jwt)){
             //构造一个po
            PasswordEntity passwordEntity=new PasswordEntity();
            BeanUtils.copyProperties(passwordEntityVo,passwordEntity);
            peService.updateByPrimaryKeySelective(passwordEntity);
            //设置re
            resultEntity.setErrCode(ResponseEnum.SUCCESS.getErrorCode());
            resultEntity.setErrorMessage(ResponseEnum.SUCCESS.getErrorMessage());
            resultEntity.setData(null);
        }else{
            //提示请登录
            //设置re
            resultEntity.setErrCode(ResponseEnum.USER_NOT_LOGIN.getErrorCode());
            resultEntity.setErrorMessage(ResponseEnum.USER_NOT_LOGIN.getErrorMessage());
            resultEntity.setData(null);
        }
        logger.info("Updateting PasswordEntity end");
        return new ResponseEntity<>(resultEntity, HttpStatus.OK);
    }



}
